<template>
  <l-panel style="padding: 0px">
    <template #toolLeft>
      <div class="l-panel--item">
        <el-input
          placeholder="输入内容"
          @keyup.enter.native="handleSearch"
          v-model="searchWord"
          size="mini"
        >
          <el-button
            @click="handleSearch"
            slot="append"
            icon="el-icon-search"
          ></el-button>
        </el-input>
      </div>
    </template>
    <l-table
      ref="mainTable"
      :loading="tableLoading"
      :columns="columns"
      :dataSource="tableData"
      :isPage="true"
      :pageTotal="tableTotal"
      :tablePage.sync="tableCurrentPage"
      @loadPageData="turnTablePage"
    >
    </l-table>
  </l-panel>
</template>
<script>
const api = window.$api.erpcase.purchase;
export default {
  props: {
    materialNumber: String,
  },
  data() {
    return {
      // query参数
      searchWord:'',

      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,
      columns: [
        { label: "采购单号", prop: "f_Number", minWidth: 120, align: "left" },
        {
          label: "采购主题",
          prop: "f_Theme",
          minWidth: 120,
          align: "left",
        },
        {
          label: "采购日期",
          prop: "f_PurchaseDate",
          width: 120,
          align: "left",
          dataType: "datetime",
          format: "yyyy-MM-dd",
        },
        {
          label: "采购人员",
          prop: "f_PurchasePerson",
          width: 100,
          align: "left",
          dataType: "user",
        },
        {
          label: "采购部门",
          prop: "f_PurchaseDep",
          align: "left",
          dataType: "department",
        },
        {
          label: "供应商",
          prop: "f_SupplierName",
          align: "left",
          dataType: "dataSource",
          dataCode: "ERP_Suppliers",
          valueKey: "f_id",
          labelKey: "f_name",
        },
        { label: "采购数量", prop: "f_CountSum", width: 100, align: "left" },
        { label: "采购金额", prop: "f_AmountSum", width: 100, align: "left" },
      ],
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.loadTableData();
    },
    handleSearch() {
      this.loadTableData();
    },

    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTableData(true);
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      let queryData = {};

      queryData.keyword = this.searchWord;
      queryData.rows = this.tablePageSize;
      queryData.page = this.tableCurrentPage;
      queryData.sidx = "f_PurchaseDate DESC";
      let data = await this.$awaitWraper(
        api.logs(this.materialNumber, queryData)
      );
      if (data != null) {
        this.tableData = data.rows;
        this.tableTotal = data.records;
      } else {
        this.tableData = [];
      }
      this.tableLoading = false;
    },
  },
};
</script>